﻿var ID = "";

$(function () {
    $('#txtFName').focusout(function () {
        
        $('#dynamicSpeakersDiv').css('visibility', 'hidden');
    });
    var uploaderImg = createUploader(
            document.getElementById('upload_img'),
            '../DataService.asmx/UploadData',
            { SpeakerID: "", insert: "" },
            ['jpg', 'png'],
            "Upload image",
            function () {
                $('#upload_img_error').css('display', 'none');
            }
         );

    var uploaderRes = createUploader(
            document.getElementById('upload_cv'),
            '../DataService.asmx/UploadData',
            { SpeakerID: "", insert: "" },
            ['pdf', 'docx'],
            "Upload CV",
            function () {
                $('#upload_file_error').css('display', 'none');
            }
         );

    $('#summaryCK').ckeditor();
    var id = querySt('id');

    if (id != "") {
        $("#btnAddOther").css("visibility", "hidden");
        var data = '{"ID": "' + id + '"}';
        ajaxCall('../DataService.asmx/GetSpeakerByID', false, data,
                function (msg) {
                    $('#txtFName').val(msg.d.FirstName);
                    $('#txtLName').val(msg.d.LastName);
                    $('#txtCName').val(msg.d.CompanyName);
                    $('#txtCountry').val(msg.d.Country);
                    $('#txtExpertIn').val(msg.d.ExpertIN);
                    $('#txtLink').val(msg.d.Link);
                    $('#summaryCK').val(msg.d.Description);
                    $('#sImage').attr("src", "../GetImageForSpeaker.ashx?id=" + id);
                    $('.SpeakerSmallImage').css("display", "inherit");
                    $('#downFile').attr("href", "../GetFileForSpeaker.ashx?id=" + id);
                    $('#downFile').css("visibility", "visible");
                },
                function () {
                    alert("Error happend while reading from database");
                }
        );

    }

    $('#btnSave').click(function (e) {
        $("#formValidateSpeaker").validate({
            rules: {
                firstname: "required",
                lastname: "required",
                country: "required",
                summaryEditor: "required"

            },
            messages: {
                firstname: "First name is required",
                lastname: "Last name is required",
                country: "Country is required",
                summaryEditor: "Description is required"

            }
        });

        CKEDITOR.instances["summaryCK"].updateElement();

        if ($("#formValidateSpeaker").valid()) {
            var data;
            if (id == "") {
                if (uploaderImg._storedFileIds.length == 0) {
                    $('#upload_img_error').css("display", "inherit");
                }
                else if (uploaderRes._storedFileIds.length == 0) {
                    $('#upload_file_error').css("display", "inherit");
                }
                else {
                    data = "{'FirstName': '" + $("#txtFName").val() + "', 'LastName': '" + $("#txtLName").val() + "' , 'CompanyName' : '" + $("#txtCName").val() + "', 'Country' : '" + $("#txtCountry").val() + "', 'ExpertIn' : '" + $("#txtExpertIn").val() + "', 'Link' : '" + $("#txtLink").val() + "', 'Description' : '" + CKEDITOR.instances["summaryCK"].getData() + "'}";
                    ajaxCall('../DataService.asmx/InsertSpeaker', false, data,
                            function (msg) {
                                ID = msg.d;
                                uploaderImg._options.params.SpeakerID = ID;
                                uploaderRes._options.params.SpeakerID = ID;
                                uploaderImg._options.params.insert = "true";
                                uploaderRes._options.params.insert = "true";
                                uploaderImg.uploadStoredFiles();
                                uploaderRes.uploadStoredFiles();
                                $('#txtFName').val("");
                                $('#txtLName').val("");
                                $('#txtCName').val("");
                                $('#txtCountry').val("");
                                $('#txtExpertIn').val("");
                                $('#txtLink').val("");
                                $('#summaryCK').val("");
                            },
                            function () {
                                alert("Error happend while inserting the data.");
                            }
                         );
                }
            }

            else {
                data = "{'ID': '" + id + "','FirstName': '" + $("#txtFName").val() + "', 'LastName': '" + $("#txtLName").val() + "' , 'CompanyName' : '" + $("#txtCName").val() + "', 'Country' : '" + $("#txtCountry").val() + "', 'ExpertIn' : '" + $("#txtExpertIn").val() + "', 'Link' : '" + $("#txtLink").val() + "', 'Description' : '" + CKEDITOR.instances["summaryCK"].getData() + "'}";
                ajaxCall('../DataService.asmx/UpdateSpeaker', false, data,
                        function (msg) {
                            ID = msg.d;
                            uploaderImg._options.params.SpeakerID = id;
                            uploaderRes._options.params.SpeakerID = id;
                            uploaderImg._options.params.insert = "false";
                            uploaderRes._options.params.insert = "false";
                            uploaderImg.uploadStoredFiles();
                            uploaderRes.uploadStoredFiles();
                            alert("Update was successful");
                            window.location = "ManageSpeakers.aspx";
                        },
                        function () {
                            alert("Error happend while updating data.");
                        }
                 );

            }
        }

    });
});

        function GetSpeakersWithThatName() {
            ajaxCall('../DataService.asmx/GetSpeakersByName', false, '{"speakerName":"' + $('#txtFName').val() + '"}',
            function (msg) {
                $('#dynamicSpeakersDiv').empty();
                if (msg.d == null) {
                    $('#dynamicSpeakersDiv').append("<strong>No speakers with such name...</strong>");
                    $('#dynamicSpeakersDiv').css('background-color', 'lightgreen');
                } else {
                    $('#dynamicSpeakersDiv').append("<strong>Check if your speaker already exists:</strong>");
                    $('#dynamicSpeakersDiv').append("<br/>");
                    $('#dynamicSpeakersDiv').append("<br/>");
                    for (i in msg.d) {
                        $('#dynamicSpeakersDiv').append(msg.d[i].FirstName + " " + msg.d[i].LastName);
                        $('#dynamicSpeakersDiv').append("<br/>");
                        $('#dynamicSpeakersDiv').css('background-color', 'lightblue');
                    }
                }
            },
            function () {

            }
        );
            $('#dynamicSpeakersDiv').css('visibility', 'visible');
        }